classdef MassOnSpring<handle
    %MassOnSpring Numerically integrates position of mass on spring
    %   Uses Euler's method
    
    properties
        k       %Spring Constant
        m       %Mass
        T0=0    %Start time
        T=10    %End time
        Dt=.01  %Time interval for propogation
        X0=10   %Starting X position
        V0=0    %Starting Y position
        X       %Results - X vs Time Step
        V       %Restuls - %This is the spring equation
        Tvec    %Time array
    end
    
    methods
        function numInt(obj)
            
            %This code taken from our script.
            %Notice the the 'obj.' so we can access our properties
            vp=@(x)-(obj.k/obj.m)*x;
            xp=@(v) v;

            obj.Tvec=(obj.T0:obj.Dt:obj.T);
            obj.X=zeros(length(obj.Tvec),1);
            obj.V=zeros(length(obj.Tvec),1);

            obj.X(1)=obj.X0;
            obj.V(1)=obj.V0;
            
            for tt=2:length(obj.Tvec)
                obj.X(tt)=obj.X(tt-1)+xp(obj.V(tt-1))*obj.Dt;
                %X(tt)=X(tt-1)+V(tt-1)*Dt;
                obj.V(tt)=obj.V(tt-1)+vp(obj.X(tt-1))*obj.Dt;
            end
            
            
        end
        
        function plotResult(obj)
            
            %This code taken from our script.
            %Notice the the 'obj.' so we can access our properties
            figure
            plot(obj.Tvec,obj.X)
            xlabel('t')
            ylabel('X(t)')
            
        end
        
    end
    
end

